package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.common.constant.ClueStatusConstants;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.domain.clue.ClueDO;
import com.qk.dto.clue.ClueDTO;
import com.qk.dto.clue.ClueListDTO;
import com.qk.entity.Clue;
import com.qk.management.mapper.ClueMapper;
import com.qk.management.service.ClueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@Service
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements ClueService {

    @Transactional

    @Override
    public PageResult<ClueDO> clueList(ClueListDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        List<ClueDO> list = this.baseMapper.selectByPage(dto);
        Page<ClueDO> page = (Page<ClueDO>) list;
        return PageResult.<ClueDO>builder()
                .total(Long.valueOf(page.getTotal()).intValue())
                .rows(page.getResult())
                .build();
    }

    @Override
    public void addClue(ClueDTO dto) {

        if (ObjectUtil.isEmpty(dto.getPhone()) || ObjectUtil.isEmpty(dto.getChannel())) {
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }

        Clue clue = BeanUtil.copyProperties(dto, Clue.class);

        clue.setCreateTime(LocalDateTime.now());

        clue.setUpdateTime(LocalDateTime.now());

        clue.setStatus(ClueStatusConstants.WAIT_ALLOCATION);

        this.save(clue);
    }

}
